//搭建仓库
import {
  legacy_createStore,//创建仓库
  applyMiddleware,//包裹中间件
} from 'redux'

import logger from 'redux-logger' //处理redux的日志
import thunk from 'redux-thunk' //处理异步的中间件

//解构
import { ALLSTATE, ALLACTTON, SETLIST, ADDCCARLIST } from '../types/store.d'


//仓库  rerx
const initialState = {
  list: [],  //定义一个数组
  carList: []  //购物车列表
}


//分别把类型约束里的type 和 payload 引入在将其约束
const reducer = (state: ALLSTATE = initialState, { type, payload }: ALLACTTON) => {


  switch (type) {

    case SETLIST:
      state.list = payload;
      return { ...state }

    case ADDCCARLIST:
      const newList = [...state.carList]
      newList.push(payload);
      state.carList = newList
      return { ...state }

    default:
      return state
  }
}

//使用legacy_createStore创建仓库  第一个参数写创建的函数 第二个写参数写中间件
const store = legacy_createStore(reducer, applyMiddleware(logger, thunk))


export default store  //抛出